package tutorial.basic_sql;

import org.apache.ibatis.session.SqlSession;
import public_util.dao.PublicMiddleTableMapper;
import public_util.model.Const4PublicDataBase;
import public_util.model.PublicMiddleTable;
import public_util.pack_4_stable.tools.DateTool;
import system_api_4_trade.dao.ApiPublicMiddleTableMapper;
import system_api_4_trade.util.sql.InsertTool;
import system_api_4_trade.util.sql.SelectTool;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;


/**
 * 中间表的增、删除、改、查，可以看招这个模式来写代码
 */
public class Tutorial4MiddleTable {



    //查询手机号码是否存在
    public static List<PublicMiddleTable> find_public_middle_table_20001(ApiPublicMiddleTableMapper midMapper, String phone)
    {
        HashMap<String, Object> paraPrice = new HashMap<String, Object>();
        paraPrice.put(Const4PublicDataBase.PARA_type_middle_app, 20001);
        paraPrice.put(Const4PublicDataBase.PARA_type_inner_app, 1001);
        paraPrice.put(Const4PublicDataBase.PARA_is_online, 0);
        paraPrice.put(Const4PublicDataBase.PARA_mid_table_remark_002+ "_in", phone);
        HashMap<String, Object>  realPara = SelectTool.paramap2selectMap(paraPrice, "public_middle_table_" + 20001);
        List<PublicMiddleTable> listQianyuePrice = midMapper.tableDataList(realPara);//查询是否具备值
        return listQianyuePrice;
    }


    /**
     * 插入table
     */
    public static void insert_contact_table(SqlSession session , ApiPublicMiddleTableMapper midMapper, String phone, HashMap<String, Object> paraPrice)
    {
        paraPrice.put(Const4PublicDataBase.PARA_type_middle_app, 20001);
        paraPrice.put(Const4PublicDataBase.PARA_type_inner_app, 1001);
        paraPrice.put(Const4PublicDataBase.PARA_is_online, 0);
        paraPrice.put(Const4PublicDataBase.PARA_mid_table_remark_002, phone);//实体的MSG, 放在这里
        paraPrice.put(Const4PublicDataBase.PARA_time_insert, DateTool.format(new Date(), "yyyy-MM-dd HH:mm:ss"));
        HashMap<String, Object> selMap = InsertTool.paramap2Insert(paraPrice, "public_middle_table_20001");
        midMapper.insertPublicMiddleTable(selMap);
        session.commit();
    }


    /**
     * 批量插入table, SQL层面上都会大动手脚的
     */
    public static void batch_insert_contact_table(SqlSession session , Tutorial4MiddleMapper midMapper, int insertLenth)
    {

        List<PublicMiddleTable> insertList = new ArrayList<>();
        for ( int i = 0; i < insertLenth; i++)
        {
            PublicMiddleTable item = new PublicMiddleTable();
            item.setType_middle_app(2000);
            item.setType_inner_app(1001);
            item.setIs_online("0");
            item.setMid_table_remark_001("001_" + i);
            item.setMid_table_remark_002("002_" + i);
            item.setMid_table_remark_003("003_" + i);
            item.setMid_table_remark_004("004_" + i);
            item.setMid_table_remark_005("005_" + i);
            item.setTime_insert(new Date());
            item.setTime_insert(new Date());
            insertList.add(item);

        }
        midMapper.bachInsert2table(insertList);
        session.commit();
    }

    //更新table
    public static void update_public_middle_table_20001(SqlSession session, String mid_table_remark_002)
    {
        HashMap<String, Object> paraData = new HashMap<String, Object>();
        paraData.put("search_type_middle_app", 20001);
        paraData.put("search_type_inner_app", 1001);
        paraData.put("search_is_online", 0);
        paraData.put(Const4PublicDataBase.PARA_mid_table_remark_002, mid_table_remark_002); // 项目ID
        paraData.put(Const4PublicDataBase.TABLE_NAME_4_MIDDLE, "public_middle_table_" + 20001);
        PublicMiddleTableMapper midMapper = session.getMapper(PublicMiddleTableMapper.class);
        midMapper.updatePublicMiddleTable(paraData);
        session.commit();
    }


    /**
     * 数据库执行下面的工作,完成MiddleTable的创建工作
     */
    public static void create_middle_table()
    {
        /**

         CREATE TABLE `public_middle_table_19001` (
         `id_public_middle_table` INT (11),
         `type_middle_app` INT (11),
         `type_inner_app` INT (11),
         `table_id_father` INT (11),
         `table_id_child` INT (11),
         `key_4_middle_table` VARCHAR (384),
         `value_4_middle_table` VARCHAR (384),
         `is_online` INT (11),
         `id_public_goods_info` INT (11),
         `buyer_id` INT (11),
         `table_status_father` INT (11),
         `table_status_child` INT (11),
         `time_4_begin` DATETIME ,
         `time_4_end` DATETIME ,
         `time_insert` DATETIME ,
         `time_update` DATETIME ,
         `table_time_child` DATETIME ,
         `table_time_father` DATETIME ,
         `data_weight` FLOAT ,
         `id_public_user_info` INT (11),
         `id_public_agent_info` INT (11),
         `id_public_data_admin_user` INT (11),
         `mid_table_remark_001` VARCHAR (384),
         `mid_table_remark_002` VARCHAR (384),
         `mid_table_remark_003` TEXT ,
         `mid_table_remark_004` VARCHAR (384),
         `mid_table_remark_005` VARCHAR (384),
         `mid_table_remark_006` VARCHAR (384),
         `mid_table_remark_007` VARCHAR (384),
         `mid_table_remark_008` VARCHAR (384),
         `mid_table_remark_009` VARCHAR (384),
         `mid_table_remark_010` VARCHAR (384),
         `mid_table_remark_011` VARCHAR (384),
         `mid_table_remark_012` VARCHAR (384),
         `mid_table_remark_013` VARCHAR (128),
         `mid_table_remark_014` VARCHAR (128),
         `mid_table_remark_015` VARCHAR (128),
         `mid_table_remark_016` VARCHAR (128),
         `mid_table_remark_017` VARCHAR (128),
         `mid_table_remark_018` VARCHAR (128),
         `mid_table_remark_019` VARCHAR (128),
         `mid_table_remark_020` VARCHAR (128),
         `mid_table_remark_021` VARCHAR (128),
         `mid_table_remark_022` VARCHAR (128),
         `mid_table_remark_023` VARCHAR (128),
         `mid_table_remark_024` VARCHAR (128),
         `mid_table_remark_025` VARCHAR (128),
         `mid_table_remark_026` VARCHAR (128),
         `mid_table_remark_027` VARCHAR (128),
         `mid_table_remark_028` VARCHAR (128),
         `mid_table_remark_029` VARCHAR (128),
         `mid_table_remark_030` VARCHAR (128),
         `mid_table_remark_031` VARCHAR (24),
         `mid_table_remark_032` VARCHAR (24),
         `mid_table_remark_033` VARCHAR (24),
         `mid_table_remark_034` VARCHAR (24),
         `mid_table_remark_035` VARCHAR (24),
         `mid_table_remark_036` VARCHAR (24),
         `mid_table_remark_037` VARCHAR (24),
         `mid_table_remark_038` VARCHAR (24),
         `mid_table_remark_039` VARCHAR (24),
         `mid_table_remark_040` VARCHAR (384),
         `mid_table_remark_041` VARCHAR (384),
         `mid_table_remark_042` VARCHAR (384),
         `mid_table_remark_043` VARCHAR (384),
         `mid_table_remark_044` VARCHAR (384),
         `mid_table_remark_045` VARCHAR (384),
         `mid_table_remark_046` VARCHAR (384),
         `mid_table_remark_047` VARCHAR (384),
         `mid_table_remark_048` VARCHAR (384),
         `mid_table_remark_049` VARCHAR (384),
         `tag_mid_table` VARCHAR (384),
         `tag_inner_mid_table` VARCHAR (384),
         `unique_key` VARCHAR (384)
         );


         ALTER TABLE `public_middle_table_19001` CHANGE `id_public_middle_table` `id_public_middle_table` INT(11) NOT NULL AUTO_INCREMENT, ADD PRIMARY KEY (`id_public_middle_table`);


         */
    }


}
